package com.sxpi.zixun.service.note;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.NoteBo;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.ex.PugValidatorException;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.NoteMapper;
import com.sxpi.zixun.pojo.Note;
import com.sxpi.zixun.vo.NoteVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Not;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/15 19:27
 */
@Service
@Slf4j
public class INoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements INoteService {

    /**
     * 查询笔记专栏管理列表信息
     * @return
     */
    @Override
    public List<NoteBo> findNoteList() {
        // 1：设置查询条件
        LambdaQueryWrapper<Note> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 2：查询发布的 0 未发布  1 发布
        lambdaQueryWrapper.eq(Note::getStatus,1);
        lambdaQueryWrapper.eq(Note::getIsdelete,0);
        // 3: 查询返回
        return tranferListBo(this.list(lambdaQueryWrapper),NoteBo.class);
    }

    /**
     * 查询笔记专栏管理列表信息并分页
     * @param noteVo
     * @return
     */
    @Override
    public IPage<NoteBo> findNotePage(NoteVo noteVo) {
        Page<Note> notePage = new Page<>(noteVo.getPageNo(),noteVo.getPageSize());
        LambdaQueryWrapper<Note> noteLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noteLambdaQueryWrapper.and(Vsserts.isNotEmpty(noteVo.getKeyword()),wrapper -> wrapper
                .like(Note::getTitle,noteVo.getKeyword())
                .or()
                .like(Note::getCategorytitle,noteVo.getKeyword())
                .or()
                .like(Note::getNickname,noteVo.getKeyword())
        );
        noteLambdaQueryWrapper.eq(Vsserts.isNotNull(noteVo.getCategoryId()),Note::getCategoryid,noteVo.getCategoryId());
        //查询发布的 0 未发布  1 发布
        noteLambdaQueryWrapper.eq(noteVo.getStatus() != null ,Note::getStatus,noteVo.getStatus());
        // 根据时间排降序
        noteLambdaQueryWrapper.orderByDesc(Note::getCreateTime);
        // 查询分页返回
        IPage<Note> results = this.page(notePage,noteLambdaQueryWrapper);
        return tranferPageBo(results,NoteBo.class);
    }

    /**
     * 根据笔记专栏管理id查询明细信息
     * @param id
     * @return
     */
    @Override
    public NoteBo getNoteById(Long id) {
        return tranferBo(this.getById(id),NoteBo.class);
    }

    /**
     * 保存和修改笔记专栏管理
     * @param note
     * @return
     */
    @Override
    public NoteBo saveUpdateNote(Note note) {
        boolean flag = this.saveOrUpdate(note);
        return flag ? tranferBo(note,NoteBo.class)  : null;
    }

    /**
     * 根据笔记专栏管理id删除笔记专栏管理
     * @param id
     * @return
     */
    @Override
    public int deleteNoteById(Long id) {
        Note adminRole = this.getById(id);
        if (adminRole != null) {
            Note note2 = new Note();
            note2.setId(id);
            note2.setIsdelete(1);
            boolean b = this.updateById(note2);
            return 1;
        }
        return 0;
    }

    /**
     * 根据笔记专栏管理ids批量删除笔记专栏管理
     * @param batchIds
     * @return
     */
    @Override
    public boolean delBatchNote(String batchIds) {
        try {
            // 1 : 把数据分割
            String[] strings = batchIds.split(",");
            // 2: 组装成一个List<Note>
            List<Note> noteList = Arrays.stream(strings).map(idstr -> {
                Note note = new Note();
                note.setId(new Long(idstr));
                note.setIsdelete(1);
                return note;
            }).collect(Collectors.toList());
            // 3: 批量删除
            return this.updateBatchById(noteList);
        } catch (Exception ex) {
            throw new PugValidatorException(ResultStatusEnum.SERVER_DB_ERROR);
        }
    }
}
